package com.android.bandroid.home;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.bandroid.R;
import com.android.bandroid.base.BaseActivity;
import com.android.bandroid.common.ConstantData;
import com.android.bandroid.home.banner.NetworkImageHolderView;
import com.android.bandroid.plugins.SlidingMenu;
import com.android.bandroid.plugins.TopBarManage;
import com.bigkoo.convenientbanner.CBViewHolderCreator;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单页
 * Created by Kevin on 2015/11/17.
 */
public class MenuActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mViewPager;
    private PagerAdapter mPagerAdapter;
    private List<View> mViews = new ArrayList<View>();
    private View mTopBar;                  //   顶部头
    private TopBarManage topBarManage;     //   顶部头管理器
    /**
     * tab的布局
     */
    private LinearLayout tabOneLlayout;
    private LinearLayout tabTwoLlayout;
    private LinearLayout tabThreeLlayout;
    private LinearLayout tabFourLlayout;

    /**
     * tab的图标
     */
    private ImageView tabOneImg;
    private ImageView tabTwoImg;
    private ImageView tabThreeImg;
    private ImageView tabFourImg;

    /**
     * tab的文字
     */
    private TextView tabOneTxt;
    private TextView tabTwoTxt;
    private TextView tabThreeTxt;
    private TextView tabFourTxt;
    /**
     * 滑动菜单
     */
    private SlidingMenu slidingMenu;
    /**
     * home页
     */
    private LinearLayout homeLlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        //初始化控件
        initView();
        //初始化点击事件
        initEvents();
        //初始化头部
        if (null != mTopBar) {
            topBarManage = new TopBarManage(mTopBar, this);
            topBarManage.initTopBarTitle("我的进销存");
            topBarManage.setLeftButtonMenu(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    slidingMenu.toggle();
                }
            });
            slidingMenu.setLeftLogoLlayout(topBarManage.getLeftLogoLlayout());//将顶部左侧头像传递
        }
        /**
         * 模块一：主页
         */
        //初始化banner
        initBanner();
        /**
         * 模块二：库存
         */
        /**
         * 模块三：资金
         */
        /**
         * 模块四：好友
         */
    }

    /**
     * 初始化banner条
     */
    private void initBanner() {
        initImageLoader();
        //网络加载例子
        networkImages = Arrays.asList(images);
        convenientBanner.setFocusable(true);
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages)
                .setPageTransformer(ConvenientBanner.Transformer.DefaultTransformer)
                .setPageIndicator(new int[]{R.mipmap.banner_page_indicator, R.mipmap.banner_page_indicator_focused});
        convenientBanner.startTurning(bannerTime);
    }

    /**
     * 初始化网络图片缓存库
     */
    private void initImageLoader() {
        //网络图片例子,结合常用的图片缓存库UIL,你可以根据自己需求自己换其他网络图片库
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().
                showImageForEmptyUri(R.mipmap.ic_launcher)
                .cacheInMemory(true).cacheOnDisk(true).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }

    /**
     * 初始化点击事件
     */
    @SuppressWarnings("deprecation")
    private void initEvents() {
        //菜单打开时点击效果
        mViewPager.setOnClickListener(this);
        //tab点击事件
        tabOneLlayout.setOnClickListener(this);
        tabTwoLlayout.setOnClickListener(this);
        tabThreeLlayout.setOnClickListener(this);
        tabFourLlayout.setOnClickListener(this);
        //viewpage滑动监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            /**
             * @param position
             */
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onPageSelected(int position) {
                int currentItem = mViewPager.getCurrentItem();
                cancelChoose();
                switch (currentItem) {
                    case ConstantData.tabOne:
                        tabOneImg.setImageResource(R.mipmap.home_b_1_1);
                        tabOneTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                        break;
                    case ConstantData.tabTwo:
                        tabTwoImg.setImageResource(R.mipmap.home_b_2_1);
                        tabTwoTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                        break;
                    case ConstantData.tabThree:
                        tabThreeImg.setImageResource(R.mipmap.home_b_3_1);
                        tabThreeTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                        break;
                    case ConstantData.tabFour:
                        tabFourImg.setImageResource(R.mipmap.home_b_4_1);
                        tabFourTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        /**
         * 菜单模块
         */
        slidingMenu = (SlidingMenu) findViewById(R.id.sliding_menu);
        mTopBar = (RelativeLayout) findViewById(R.id.top_bar_home);
        tabOneLlayout = (LinearLayout) findViewById(R.id.llayout_tab_1);
        tabTwoLlayout = (LinearLayout) findViewById(R.id.llayout_tab_2);
        tabThreeLlayout = (LinearLayout) findViewById(R.id.llayout_tab_3);
        tabFourLlayout = (LinearLayout) findViewById(R.id.llayout_tab_4);
        tabOneImg = (ImageView) findViewById(R.id.img_tab_1);
        tabTwoImg = (ImageView) findViewById(R.id.img_tab_2);
        tabThreeImg = (ImageView) findViewById(R.id.img_tab_3);
        tabFourImg = (ImageView) findViewById(R.id.img_tab_4);
        tabOneTxt = (TextView) findViewById(R.id.txt_tab_1);
        tabTwoTxt = (TextView) findViewById(R.id.txt_tab_2);
        tabThreeTxt = (TextView) findViewById(R.id.txt_tab_3);
        tabFourTxt = (TextView) findViewById(R.id.txt_tab_4);
        //装载viewpager切换布局
        mViewPager = (ViewPager) findViewById(R.id.pager_home);
        LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View pager_erp = mLayoutInflater.inflate(R.layout.pager_erp, null);
        View pager_inventory = mLayoutInflater.inflate(R.layout.pager_inventory, null);
        View pager_fund = mLayoutInflater.inflate(R.layout.pager_fund, null);
        View pager_im = mLayoutInflater.inflate(R.layout.pager_im, null);
        pager_erp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggleContent();
            }
        });
        pager_inventory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggleContent();
            }
        });
        pager_fund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggleContent();
            }
        });
        pager_im.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggleContent();
            }
        });
        mViews.add(pager_erp);
        mViews.add(pager_inventory);
        mViews.add(pager_fund);
        mViews.add(pager_im);
        mPagerAdapter = new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mViews.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mViews.get(position));
            }

            @Override
            public int getCount() {
                return mViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        };
        mViewPager.setAdapter(mPagerAdapter);
        /**
         * 模块一：主页
         */
        convenientBanner = (ConvenientBanner) pager_erp.findViewById(R.id.convenientBanner);
        /**
         * 模块二：库存
         */
        /**
         * 模块三：资金
         */
        /**
         * 模块四：好友
         */
    }

    /**
     * tab被点击
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        cancelChoose();
        switch (v.getId()) {
            case R.id.llayout_tab_1:
                mViewPager.setCurrentItem(ConstantData.tabOne);
                tabOneImg.setImageResource(R.mipmap.home_b_1_1);
                tabOneTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                break;
            case R.id.llayout_tab_2:
                mViewPager.setCurrentItem(ConstantData.tabTwo);
                tabTwoImg.setImageResource(R.mipmap.home_b_2_1);
                tabTwoTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                break;
            case R.id.llayout_tab_3:
                mViewPager.setCurrentItem(ConstantData.tabThree);
                tabThreeImg.setImageResource(R.mipmap.home_b_3_1);
                tabThreeTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                break;
            case R.id.llayout_tab_4:
                mViewPager.setCurrentItem(ConstantData.tabFour);
                tabFourImg.setImageResource(R.mipmap.home_b_4_1);
                tabFourTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_press));
                break;

        }
    }

    /**
     * 将tab选项清空
     */
    private void cancelChoose() {
        tabOneImg.setImageResource(R.mipmap.home_b_1_0);
        tabTwoImg.setImageResource(R.mipmap.home_b_2_0);
        tabThreeImg.setImageResource(R.mipmap.home_b_3_0);
        tabFourImg.setImageResource(R.mipmap.home_b_4_0);
        tabOneTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_normal));
        tabTwoTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_normal));
        tabThreeTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_normal));
        tabFourTxt.setTextColor(getResources().getColor(R.color.tab_txt_color_normal));
    }

    /**
     * 模块一：主页
     */
    private ConvenientBanner convenientBanner;//顶部广告栏控件
    private ArrayList<Integer> localImages = new ArrayList<Integer>();
    private List<String> networkImages;
    private long bannerTime = 3000;  //banner翻页时间
    private String[] images = {
            "http://img2.3lian.com/2014/f2/37/d/40.jpg",
            "http://d.3987.com/sqmy_131219/001.jpg",
            "http://img2.3lian.com/2014/f2/37/d/39.jpg",
            "http://www.8kmm.com/UploadFiles/2012/8/201208140920132659.jpg",
            "http://f.hiphotos.baidu.com/image/h%3D200/sign=1478eb74d5a20cf45990f9df460b4b0c/d058ccbf6c81800a5422e5fdb43533fa838b4779.jpg",
            "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg"
    };
    /**
     * 模块二：库存
     */
    /**
     * 模块三：资金
     */
    /**
     * 模块四：好友
     */

    /**
     *
     */
    // 开始自动翻页
    @Override
    protected void onResume() {
        super.onResume();
        //开始自动翻页
        convenientBanner.startTurning(bannerTime);//此值不能小于1200（即ViewPagerScroller的mScrollDuration的值），否则最后一页翻页效果会出问题。如果硬要兼容1200以下，那么请修改ViewPagerScroller的mScrollDuration的值，不过修改后，3d效果就没那么明显了。
    }

    // 停止自动翻页
    @Override
    protected void onPause() {
        super.onPause();
        //停止翻页
        convenientBanner.stopTurning();
    }
}
